<?php

namespace App\Http\Controllers\Api;

use App\Services\Api\UserAppService;
use App\Services\VerificationCodeService;
use Exception;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AppController extends Controller
{
    private $appService;

    public function __construct(UserAppService $appService)
    {
        $this->appService = $appService;
    }

    /**
     * c端用户登录
     * @param Request $request
     * @return array
     */
    public function login(Request $request)
    {
        try {
            $validateRules = [
                'jsCode'       => 'required',
            ];
            $validateMessages = [
                'jsCode.required' => '微信动态code不能为空!'
            ];

            $this->requestValidator($request, $validateRules, $validateMessages);
            $jsCode = $request->input('jsCode');

            $data = $this->appService->login($jsCode);

            return $this->wrapSuccessReturn(compact('data'));
        } catch (Exception $exception) {
            return $this->wrapErrorReturn($exception);
        }
    }

    /**
     * 验证码发送
     * @param Request $request
     * @return array
     */
    public function loginSendCode(Request $request)
    {
        try {

            $validatorRules = [
                'phone' => 'required', //用户名
            ];

            $validatorMessages = [
                'phone.required' => "手机号不能为空错误!",
            ];

            $this->requestValidator($request, $validatorRules, $validatorMessages);

            $phone = $request->input('phone');

            $data = $this->appService->loginSendCode($phone);

            return $this->wrapSuccessReturn(compact('data'));
        } catch (\Exception $exception) {
            return $this->wrapErrorReturn($exception);
        }
    }

    /**
     * 验证手机验证码
     * @param Request $request
     * @return array
     */
    public function checkCode(Request $request)
    {

        try {

            $phone = $request->input('phone');
            $code  = $request->input('code');
            $local = $request->input('location');
            /**
             * 验证短信验证码
             * @var VerificationCodeService $verificationCodeService
             */
            $verificationCodeService = app(VerificationCodeService::class);
            $verificationCodeService->checkVerificationCode($phone, $code);
            $data = $this->appService->savePhone($phone,$local);
            return $this->wrapSuccessReturn(compact('data'));
        } catch (\Exception $exception) {
            return $this->wrapErrorReturn($exception);
        }


    }

    /**
     * 获取商户基础信息
     * @param Request $request
     * @return array
     */
//    public function merchantDetail(Request $request)
//    {
//        try {
//
//            $validateRules = [
//                'merchantCode' => 'required'
//            ];
//            $validateMessages = [
//                'merchantCode.required' => '商店码不能为空!'
//            ];
//
//            $this->requestValidator($request, $validateRules, $validateMessages);
//            $merchantCode = $request->input('merchantCode');
//
//
//            $data = $this->appService->merchantDetail($merchantCode);
//
//            return $this->wrapSuccessReturn(compact('data'));
//        } catch (Exception $exception) {
//            return $this->wrapErrorReturn($exception);
//        }
//    }
}
